package edu.usc.libtalk;

import edu.usc.libtalk.utils.ApplicationService;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SignInWithNameActivity extends Activity implements OnTouchListener{	
	private String library;
	public static Activity signInWithNameActivity;
	private RelativeLayout thisLayout;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Customize menu title 
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        
        setContentView(R.layout.activity_sign_in_with_name);
        
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
        
        // Help button Handler    	
    	Button help_bn = (Button) findViewById(R.id.help_button);
    	help_bn.setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    			helpDialog();
    		}
    	});  
        
        library = ApplicationService.getLibrary();
        String message = "Current library: " + library;
        
        TextView tv = (TextView) findViewById(R.id.sigin_lib_name);
        tv.setText(message);  
    	
    	thisLayout = (RelativeLayout) findViewById(R.id.sign_in_with_name_view);
    	thisLayout.setOnTouchListener(this);
    }
    
    /* Called when the user clicks the "Confirm" button */
    public void setNameHandler(View view) {
    	EditText edittext_username = (EditText) findViewById(R.id.signin_wn_edittext_username);
    	EditText editText_interest = (EditText) findViewById(R.id.signin_wn_edittext_interest);
    	  	
    	if((edittext_username.getText().toString().length() == 0) && (editText_interest.getText().toString().length() > 0))
    	{
    		ApplicationService.inform("Please input user name");
    	} 
    	else if((edittext_username.getText().toString().length() > 0) && (editText_interest.getText().toString().length() == 0))
    	{	
    		ApplicationService.inform("Please input your area of interest");
    	}
    	else if((edittext_username.getText().toString().length() == 0) && (editText_interest.getText().toString().length() == 0))
    	{
    		ApplicationService.inform("Please input user name and your area of interest");    	
    	} else
    	{
	    	Intent intent = new Intent(SignInWithNameActivity.this, SignInWithLocActivity.class);            	
	    	ApplicationService.setUserName(edittext_username.getText().toString());
	    	ApplicationService.setAreaOfInterest(editText_interest.getText().toString());
	    	startActivity(intent);
    	}
    }

    /* Called when the user clicks the "Clear" button */
    public void clearNameHandler(View view) {
    	EditText editText = (EditText) findViewById(R.id.signin_wn_edittext_username);
    	editText.setText("");
    	editText = (EditText) findViewById(R.id.signin_wn_edittext_interest);
    	editText.setText("");
    	ApplicationService.inform("Clear text inputs");
    }
    
    private void helpDialog()
    {
    	AlertDialog alertDialog = new AlertDialog.Builder(SignInWithNameActivity.this).create();
		
		// Setting Dialog Title
		alertDialog.setTitle("Help");
		
		// Setting Dialog Message
		alertDialog.setMessage("Step 1 : Select the Library.\n\nStep 2 : Login as Registered (with Name and Interest) or Anonymous User.\n\nStep 3 : If you are Registered user, then select your seat (free spot) on any level of that selected library.\n\nStep 4 : Click on the \"online users\" tab to see all the online users of that particular library with their \"Area of Interest\". Click any one from the list and start chat.\n\nStep 5 : You can also go to all the levels and can select users and start chat.\n\nStep 6 : After you are done chatting, click on the \"Sign out\" button on the header. Here you can also send the entire chat of your message board to your email.\n\nStep 7 : Happy Chatting ...");

		// Setting OK Button
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int which) {
		    	// Write your code here to execute after dialog closed
		    		
		    }
		});
		
		alertDialog.show();
    }

	@Override
	public boolean onTouch(View v, MotionEvent me) {
		EditText username = (EditText) findViewById(R.id.signin_wn_edittext_username);
    	EditText interest = (EditText) findViewById(R.id.signin_wn_edittext_interest);
    	// All we're doing is hiding the keyboard if it has been displayed
		if (me.getAction() == MotionEvent.ACTION_DOWN) {
			InputMethodManager imm = (InputMethodManager)getSystemService(
				Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(username.getWindowToken(), 0);
				imm.hideSoftInputFromInputMethod(interest.getWindowToken(), 0);
		}
		return true;
	}    
}
